package com.easybuilder.blog.controller;

import com.easybuilder.blog.dto.BlogFansDTO;
import com.easybuilder.blog.entity.BlogFans;
import com.easybuilder.blog.service.FansService;
import com.easybuilder.common.base.controller.BaseController;
import com.easybuilder.common.base.entity.Page;
import com.easybuilder.common.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


@RestController
@RequestMapping("/fans")
public class FansController extends BaseController {

    @Autowired
    private FansService fansService;

    /** 关注 */
    @PostMapping("/attention/{userId}")
    public Result<?> annotation(@PathVariable(name = "userId", required = true) Long userId) {
        int annotation = fansService.attention(userId);
        return OK(annotation > 0);
    }

    /** 取消关注 */
    @PostMapping("/attention/cancel/{userId}")
    public Result<?> cancelAnnotation(@PathVariable(name = "userId", required = true) Long userId) {
        int annotation = fansService.cancelAttention(userId);
        return OK(annotation > 0);
    }

    /** 粉丝列表分页查询 */
    @PostMapping("/page")
    public Result<?> fansPage(HttpServletRequest request) {
        int pageNum = StringUtil.isEmpty(request.getHeader("page_num")) ? 0 : Integer.parseInt(request.getHeader("page_num"));
        int pageSize = StringUtil.isEmpty(request.getHeader("page_size")) ? 10 : Integer.parseInt(request.getHeader("page_size"));
        Page<BlogFansDTO> page = fansService.fansPage(pageNum, pageSize);
        return OK(page);
    }

    /** 关注列表分页查询 */
    @PostMapping("/attention/page")
    public Result<?> attentionPage(HttpServletRequest request) {
        int pageNum = StringUtil.isEmpty(request.getHeader("page_num")) ? 0 : Integer.parseInt(request.getHeader("page_num"));
        int pageSize = StringUtil.isEmpty(request.getHeader("page_size")) ? 10 : Integer.parseInt(request.getHeader("page_size"));
        Page<BlogFans> page = fansService.attentionPage(pageNum, pageSize);
        return OK(page);
    }

}
